/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scene;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;

/**
 * Created by JiangQi on 7/30/18.
 *
 * @hide
 **/
public class LifeCycleFragment extends Fraction {
    private LifecycleFragmentDetachCallback mLifecycleFragmentDetachCallback = null;
    private SceneContainerLifecycleCallback mSceneContainerLifecycleCallback = null;

    /**
     * 单例
     *
     * @param supportRestore
     * @return fragment
     */
    public static LifeCycleFragment newInstance(boolean supportRestore) {
        LifeCycleFragment fragment = new LifeCycleFragment();
        return fragment;
    }

    /**
     * fraction生命周期
     *
     * @author:
     * @since 2021-05-19
     */
    public static interface LifecycleFragmentDetachCallback {
        /**
         * onDetach
         */
        void onDetach();
    }

    void setLifecycleFragmentDetachCallback(LifecycleFragmentDetachCallback lifecycleFragmentDetachCallback) {
        this.mLifecycleFragmentDetachCallback = lifecycleFragmentDetachCallback;
    }

    public void setSceneContainerLifecycleCallback(SceneContainerLifecycleCallback callback) {
        this.mSceneContainerLifecycleCallback = callback;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        if (this.mSceneContainerLifecycleCallback != null) {
            return this.mSceneContainerLifecycleCallback.onComponentAttached(getFractionAbility(),scatter, container,intent);
        } else {
            removeSelfWhenNavigationSceneUtilityIsNotInvoked();
            return null;
        }
    }

    private void removeSelfWhenNavigationSceneUtilityIsNotInvoked() {
        FractionManager fractionManager = getFractionAbility().getFractionManager();
        fractionManager.startFractionScheduler().remove(LifeCycleFragment.this).submit();
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        if (this.mSceneContainerLifecycleCallback != null) {
            this.mSceneContainerLifecycleCallback.onStart(getFractionAbility(),intent);
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
        if (this.mSceneContainerLifecycleCallback != null) {
            this.mSceneContainerLifecycleCallback.onActive();
        }
    }


    @Override
    protected void onBackground() {
        super.onBackground();
        if (this.mSceneContainerLifecycleCallback != null) {
            this.mSceneContainerLifecycleCallback.onBackground();
        }
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        if (this.mSceneContainerLifecycleCallback != null) {
            this.mSceneContainerLifecycleCallback.onForeground(intent);
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if (this.mSceneContainerLifecycleCallback != null) {
            this.mSceneContainerLifecycleCallback.onInactive();
        }
    }


    @Override
    public void onStop() {
        super.onStop();
        if (this.mSceneContainerLifecycleCallback != null) {
            this.mSceneContainerLifecycleCallback.onStop();
        }
    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
        if (this.mSceneContainerLifecycleCallback != null) {
            this.mSceneContainerLifecycleCallback.onComponentDetach();
        }
    }
}
